<?php

class CommentController extends BaseController {
	public function postComment($lang, $newsID) {
		require_once(app_path().'/lib/recaptchalib.php');
	  	$cond = false;
		
		$privatekey = Options::CAPTCHA_PRIVATE_KEY;
	  	$resp = recaptcha_check_answer ($privatekey,
	    	                        $_SERVER["REMOTE_ADDR"],
	                                $_POST["recaptcha_challenge_field"],
	                                $_POST["recaptcha_response_field"]);
	  	
		
	  if (!$resp->is_valid) {
	    $status = Options::PAGE_NEWS_DETAILS_COMMENT_CAPTCHA_ERROR;	    
	  } else {
	    $status = Options::PAGE_NEWS_DETAILS_COMMENT_CAPTCHA_OK;
	    $cond = true;
	  }
	  
	  //check message if previous cond is true
	  if( $cond ) {
		  if( empty( Input::get('txtMessage') ) ) {
		  	$status =  Options::PAGE_NEWS_DETAILS_COMMENT_MESSAGE_BLANK;
		  	$cond = false;
		  } 
	  }
	  
	  //save the comment if cond is true
	  if( $cond ) {
	  	$comment = new Comment();
	  	$comment->newsID = $newsID;
	  	$comment->commenter = empty( Input::get('txtUserName') ) ? Lang::get( 'label.anonymous') : Input::get('txtUserName') ;
	  	$comment->comment = Input::get('txtMessage');
	  	$comment->useragent = $_SERVER['HTTP_USER_AGENT'];
	  	$comment->addNew();
	  		  	
	  	$status = Options::PAGE_NEWS_DETAILS_COMMENT_STATUS_POSTED;
	  }
	  
	  $news = News::newsID($newsID)->get(array(Options::DB_TABLE_NEWS.'.title'));
	  
	  $newsTitle = !empty($news) ? $news[0]->title : "n";
	  
	  return Redirect::route('newsDetails', array('lang' => $lang,'id' => $newsID,'title' => SEO::customURLEncode($newsTitle),'status' => $status));
	} 
	
	}